package org.robot.scm.platform.wxrobot;

import com.google.common.collect.Lists;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.infra.RedisCacheHelper;
import org.robot.scm.platform.ReflectionLoader;
import org.robot.scm.platform.wxrobot.processor.EatVoteRobotProcessor;
import org.robot.scm.platform.wxrobot.processor.TelegramRobotProcessor;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.platform.MessageBaseNotify;
import org.robot.scm.utils.SendNoticeHelper;
import org.robot.scm.utils.StreamUtils;

/**
 * @Author : Awn create on 2024/4/22 15:48
 */
@Slf4j
public abstract class AbstractRobotProcessor {

    public static final List<? extends AbstractRobotProcessor> instances = ReflectionLoader.load(AbstractRobotProcessor.class);

    public abstract List<String> getKeywords(BaseConfig config);

    protected abstract List<TargetNotify> process(BaseConfig config, String sendTo, String msg);

    public static boolean doNext(BaseConfig config, String sendTo, String msg) {
        List<? extends AbstractRobotProcessor> processors = StreamUtils.filter(instances, e -> e.getKeywords(config).contains(msg));
        if (CollectionUtils.isEmpty(processors)) {
            return false;
        }

        for (AbstractRobotProcessor processor : processors) {
            // 娱乐类的消息需要限流
            if (!(processor instanceof TelegramRobotProcessor) && !(processor instanceof EatVoteRobotProcessor)) {
                if (StringUtils.isNotBlank(RedisCacheHelper.get(sendTo))) {
                    MessageBaseNotify msgBean = MessageBaseNotify.builder()
                            .message("哥哥, 动作慢点\uD83D\uDC44")
                            .deviceSn(sendTo)
                            .build();
                    SendNoticeHelper.sendNotice(Lists.newArrayList(msgBean.robotToNotifyBean(sendTo, config)));
                    continue;
                }
                RedisCacheHelper.put(sendTo, "true", 5);
            }
            List<TargetNotify> targetNotifies = processor.process(config, sendTo, msg);
            SendNoticeHelper.sendNotice(targetNotifies);
        }

        return true;
    }
}
